package com.hdhere.application.controller.admin;

import com.hdhere.application.service.VideoCategoryService;
import com.hdhere.domain.common.Pagination;
import com.hdhere.domain.common.rest.CallResult;
import com.hdhere.domain.media.VideoCategory;
import com.hdhere.domain.media.page.VideoCategoryPagination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/videoCate")
public class VideoCategoryController {

    private static Logger logger = LoggerFactory.getLogger(VideoCategoryController.class);

    @Autowired
    private VideoCategoryService categoryService;

    @GetMapping
    public String list(){
        return "admin/videoCateList";
    }
    @PostMapping
    @ResponseBody
    public Pagination listPage(VideoCategoryPagination input){
        input.matchPage();
        logger.info("this is a info {}",input);
        Pagination page = categoryService.getPage(input);
        return page;
    }
    @PostMapping("/list")
    @ResponseBody
    public List<VideoCategory> listAll(){

        return categoryService.getALL();
    }

    @GetMapping({"/save/{id}","/save"})
    public String save(@PathVariable(required = false) Long id, Map<String, Object> map){

        var cate = id == null ? new VideoCategory(0L, "/0")
                : categoryService.getOne(id);
        map.put("c", cate);
        return "admin/videoCateSave";
    }
    @PostMapping(value = "/save", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public CallResult save(@RequestBody VideoCategory cate){
        categoryService.save(cate);
        return new CallResult(cate.getId());
    }

    @PostMapping(value = "/del", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public CallResult del(@RequestParam Long[] id){
        var n = categoryService.delete(id);
        return new CallResult(n);
    }
}
